package cn.xuewei.controller;

import cn.xuewei.mapper.MessageMapper;
import cn.xuewei.mapper.SessionListMapper;
import cn.xuewei.pojo.entity.Message;
import cn.xuewei.pojo.entity.SessionList;
import cn.xuewei.response.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/v1/message/")
public class MessageController {

    @Autowired(required = false)
    private MessageMapper messageMapper;

    @Autowired(required = false)
    private SessionListMapper sessionListMapper;

    @GetMapping("messageList")
    public ResultVO messageList(@RequestParam Integer sessionId){
        //获取会话列表
        SessionList sessionList = sessionListMapper.selectByPrimaryKey(sessionId);

        if(sessionList == null){
            return ResultVO.ok();
        }

        Integer fromUserId = sessionList.getUserId();
        Integer toUserId = sessionList.getToUserId();
        List<Message> messageList = messageMapper.selectMessageList(fromUserId,toUserId);

        //更新消息已读
        messageMapper.messageRead(fromUserId,toUserId);

        //更新会话里面的未读消息
        sessionListMapper.deleteUnReadCount(fromUserId,toUserId);

        System.out.println(ResultVO.ok(messageList));
        return ResultVO.ok(messageList);
    }

}
